/*
 * @Date: 2025-07-19 23:08:05
 * @LastEditTime: 2025-07-23 21:42:59
 */
import { defineStore } from "pinia";

interface IAccountProps {
  userId: string;
  username: string;
  account: string;
  // 0: 用户 1：管理员
  role: number;
}

interface StateProps {
  account: IAccountProps;
  // 登录时间
  loginTime: number;
}

export const useAccountStore = defineStore("ACCOUNT_STORE", {
  state: (): StateProps => {
    return {
      account: {} as IAccountProps,
      loginTime: 0,
    };
  },
  getters: {
    /** 是否过期 */
    isExpired: (state) => {
      // 判断是否过期, 默认12小时
      return Date.now() - state.loginTime > 1000 * 60 * 60 * 12;
    },
  },
  actions: {
    /**
     * 设置账号信息
     * @param account 账号信息
     */
    setAccount(account: IAccountProps) {
      const acc = { ...account };
      this.account = acc;
      this.loginTime = Date.now();
    },
    /**
     * 登出
     * */
    logout() {
      this.account = {} as IAccountProps;
      sessionStorage.clear();
    },
  },
  persist: {
    storage: sessionStorage,
  },
});
